var url="";
var model;
var request;
var Index;
var result=false;
function LoadData(){
  result=false;
    request.post('http://'+url+'/app/ProjectOverview!ajaxProjectOverview.action').success(function(data){
        console.log(data);
         //model.list = data;
           model.detail =data;
           model.arealist = data.areaList;
           model.industryList = data.industryList;
      LoadList();
         }).error(function(data,status,headers,config){
      if((status>=200&&status<300)||status===304||status===1223||status===0){
      $("body").html("网络访问出错！");
      }
      });
}

function LoadList(){
    request.post('http://'+url+'/app/ProjectOverview!ajaxProjectList?area_id='+model.areaType+'&industry_id='+model.industryType+'&orderType='+model.orderType+'&energyCostType='+model.energyCostType).success(function(data){
        console.log(data);
         //model.list = data;
           model.list=data;
           // model.list.push(data);
           // model.list.push(data);
            result=true;
         }).error(function(data,status,headers,config){
      if((status>=200&&status<300)||status===304||status===1223||status===0){
      $("body").html("网络访问出错！");
      }
      });
}

function LoadWeather(){
        request.post('http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&day=0&city=&dfc=1&charset=utf-8').success(function(data){
        var index1 = data.indexOf("]=[");
        var index2 = data.indexOf("}];var");
        var index3 = data.indexOf(";w['");
        if(index1!=-1&&index2!=-1&&index3!=-1)
        {
        var ds = data.substring(index1+2);
        var ds2 = data.substring(index1+2,index2+2);
        var ds3 = data.substring(index3+4,index1-1);
        var obj = eval("("+ds2+")");
        Index.city = ds3;
        Index.weather = obj[0];
        Index.temp = parseInt((parseInt(Index.weather.t2)+parseInt(Index.weather.t1))/2);
        var myDate = new Date();
        myDate.getHours();
        var t = "_0";
        if((myDate.getHours()>=0&&myDate.getHours()<=6) || (myDate.getHours()>=18&&myDate.getHours()<=23))
        {
          t = "_1"
        }
        Index.weatherurl = "http://php.weather.sina.com.cn/images/yb3/78_78/"+Index.weather.f1+t+".png";

        }


         }).error(function(data,status,headers,config){
      if((status>=200&&status<300)||status===304||status===1223||status===0){
      $("body").html("网络访问出错！");
      }
      });
}

(function(){
	var app=angular.module('Index',[ ]);
      app.config(function($httpProvider) {
        $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
        $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
        
    // Override $http service's default transformRequest
    $httpProvider.defaults.transformRequest = [function(data) {
        /**
         * The workhorse; converts an object to x-www-form-urlencoded serialization.
         * @param {Object} obj
         * @return {String}
         */
         var param = function(obj) {
            var query = '';
            var name, value, fullSubName, subName, subValue, innerObj, i;
            
            for (name in obj) {
                value = obj[name];
                
                if (value instanceof Array) {
                    for (i = 0; i < value.length; ++i) {
                        subValue = value[i];
                        fullSubName = name + '[' + i + ']';
                        innerObj = {};
                        innerObj[fullSubName] = subValue;
                        query += param(innerObj) + '&';
                    }
                } else if (value instanceof Object) {
                    for (subName in value) {
                        subValue = value[subName];
                        fullSubName = name + '[' + subName + ']';
                        innerObj = {};
                        innerObj[fullSubName] = subValue;
                        query += param(innerObj) + '&';
                    }
                } else if (value !== undefined && value !== null) {
                    query += encodeURIComponent(name) + '='
                    + encodeURIComponent(value) + '&';
                }
            }
            
            return query.length ? query.substr(0, query.length - 1) : query;
        };
        
        return angular.isObject(data) && String(data) !== '[object File]'
        ? param(data)
        : data;
    }];
});
	app.controller('IndexController',['$http','$window','$scope',function($http,$window,$scope){
        Index=this;
		request=$http;
    model=Index;
    url ='120.26.110.125/jncloud';
    Index.orderType = 1;

    bindEvent();
    Index.areaType = '';
    Index.areaTypeIndex=-1;

    Index.industryType='';
    Index.industryTypeIndex=-1;


    Index.energyCostType='';
    Index.energyCostTypeIndex=-1;

    Index.showOrderMenu = {'display':'none'};
    Index.showOrderMenu2 = {'display':'none'};
    Index.showMenuDing={'display':'none'};
    Index.shaixuan = {'display':'none'};
    Index.shaixuan2 = {'display':'none'};
    LoadData();
    LoadWeather();



    Index.showOrder=function(){

          if(Index.showOrderMenu.display=='none')
          Index.showOrderMenu = {'display':''};
          else
          Index.showOrderMenu = {'display':'none'}; 

    };

    Index.showOrder2=function(){
        
          if(Index.showOrderMenu2.display=='none')
          Index.showOrderMenu2 = {'display':''};
          else
          Index.showOrderMenu2 = {'display':'none'}; 

    };

    Index.showShaixuan=function(){
          if(Index.shaixuan.display=='none')
          Index.shaixuan = {'display':''};
          else
          Index.shaixuan = {'display':'none'}; 
    }

    Index.showShaixuan2=function(){
          if(Index.shaixuan2.display=='none')
          Index.shaixuan2 = {'display':''};
          else
          Index.shaixuan2 = {'display':'none'}; 
    }


Index.gotoProject=function(id){
  //alert(id);
  client.gotoProject(id);
};

Index.change1=function(){
  $scope.$apply(function(){
        if(Index.showMenuDing.display == 'none'){
          Index.showMenuDing={'display':''};
           
              if(Index.showOrderMenu2.display=='')
              {
                Index.showOrderMenu2 = {'display':'none'};
                Index.showOrderMenu = {'display':''};
              }

              if(Index.shaixuan2.display=='')
              {
                Index.shaixuan2 = {'display':'none'};
                Index.shaixuan = {'display':''};
              }        
 
        }       
  });
}

Index.change2=function(){
    $scope.$apply(function(){
     
        if(Index.showMenuDing.display ==''){
          Index.showMenuDing={'display':'none'};
              if(Index.showOrderMenu.display=='')
              {
                Index.showOrderMenu = {'display':'none'};
                Index.showOrderMenu2 = {'display':''};
              }


              if(Index.shaixuan.display=='')
              {
                Index.shaixuan = {'display':'none'};
                Index.shaixuan2 = {'display':''};
              } 

          }
  });
}


Index.HideMenu=function(type){
       Index.orderType = type;
       Index.showOrderMenu2 = {'display':'none'};
       Index.showOrderMenu = {'display':'none'};
       LoadList();
}


Index.listshai=function(index,type){
      if(type!=-1){
      Index.industryType = type
    } else Index.industryType = '';

    Index.industryTypeIndex = index;

}

Index.listshai2=function(index,type){
    if(type!=-1){
      Index.areaType = type
    } else Index.areaType = '';

    Index.areaTypeIndex = index;
}

Index.listshai3=function(index,type){
      if(type!=-1){
      Index.energyCostType = type
    } else Index.energyCostType = '';

    Index.energyCostTypeIndex = index;
}

Index.reset=function(){
      Index.industryTypeIndex = -1;
      Index.areaTypeIndex = -1;
      Index.energyCostTypeIndex = -1;
      Index.industryType='';
      Index.areaType = '';
      Index.energyCostType = '';
}

Index.HideShaiMenu=function(){
  Index.shaixuan = {'display':'none'};
  Index.shaixuan2 = {'display':'none'};
  LoadList();

}

     $window.onscroll = function () { 
         console.log("au2");
      var top = document.documentElement.scrollTop || document.body.scrollTop;
      if(top>=$("#menu1").offset().top){
         Index.change1();

      }else{

        Index.change2();

      }
    };
	}]);
})();


function EncodeUtf8(s1)
{
      var s = escape(s1);
      var sa = s.split("%");
      var retV ="";
      if(sa[0] != "")
      {
         retV = sa[0];
      }
      for(var i = 1; i < sa.length; i ++)
      {
           if(sa[i].substring(0,1) == "u")
           {
               retV += Hex2Utf8(Str2Hex(sa[i].substring(1,5)));
              
           }
           else retV += "%" + sa[i];
      }
     
      return retV;
}

function Str2Hex(s)
{
      var c = "";
      var n;
      var ss = "0123456789ABCDEF";
      var digS = "";
      for(var i = 0; i < s.length; i ++)
      {
         c = s.charAt(i);
         n = ss.indexOf(c);
         digS += Dec2Dig(eval(n));
          
      }
      //return value;
      return digS;
}
function Dec2Dig(n1)
{
      var s = "";
      var n2 = 0;
      for(var i = 0; i < 4; i++)
      {
         n2 = Math.pow(2,3 - i);
         if(n1 >= n2)
         {
            s += '1';
            n1 = n1 - n2;
          }
         else
          s += '0';
         
      }
      return s;
     
}
function Dig2Dec(s)
{
      var retV = 0;
      if(s.length == 4)
      {
          for(var i = 0; i < 4; i ++)
          {
              retV += eval(s.charAt(i)) * Math.pow(2, 3 - i);
          }
          return retV;
      }
      return -1;
}

function Hex2Utf8(s)
{
     var retS = "";
     var tempS = "";
     var ss = "";
     if(s.length == 16)
     {
         tempS = "1110" + s.substring(0, 4);
         tempS += "10" + s.substring(4, 10);
         tempS += "10" + s.substring(10,16);
         var sss = "0123456789ABCDEF";
         for(var i = 0; i < 3; i ++)
         {
            retS += "%";
            ss = tempS.substring(i * 8, (eval(i)+1)*8);
           
           
           
            retS += sss.charAt(Dig2Dec(ss.substring(0,4)));
            retS += sss.charAt(Dig2Dec(ss.substring(4,8)));
         }
         return retS;
     }
     return "";
} 
